package com.wf.demo.info.web;

import com.wf.demo.info.pojo.Device;
import com.wf.demo.info.service.DeviceService;
import com.wf.demo.info.util.Page4Navigator;
import com.wf.demo.info.util.Result;
import com.wf.demo.sys.annotation.SysLog;
import com.wf.demo.sys.constant.CommonConstant;
import com.wf.demo.sys.pojo.User;
import com.wf.demo.sys.service.UserService;
import com.wf.demo.sys.util.CommonUtils;
import com.wf.demo.sys.util.JwtUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequiresPermissions("info:device")
public class DeviceController {
    @Autowired
    DeviceService deviceService;
    @Autowired
    UserService userService;
    //公司下所有设备
    @GetMapping("/companys/{cid}/devices")
    @RequiresPermissions("info:device")
    public Page4Navigator<Device> list(@PathVariable("cid")int cid, @RequestParam(value = "start",defaultValue = "0")int start,
                                       @RequestParam(value = "size",defaultValue = "10")int size) throws Exception{
        start = start<0? 0:start;
        Page4Navigator<Device> page = deviceService.list(cid,start,size,5);
        return page;
    }
    @GetMapping("/devices/{id}")
    @RequiresPermissions("info:device")
    public Object get(@PathVariable("id") Long id) throws Exception {


        Device bean= deviceService.get(id);
        return bean;
    }
    @SysLog("保存设备")
    @PostMapping("/devices")
    @RequiresPermissions("info:device")
    public Object add(@RequestBody Device bean) throws Exception {
        String csNumber=bean.getCsNumber();
        long beidouId=bean.getBeidouId();
        int existCode=deviceService.isExist(csNumber,beidouId);
        if (existCode==1){
            String message ="控制系统编号已经被使用,添加失败";
            return Result.addFail(message);
        }
        if (existCode==2){
            String message ="北斗卡号已经被使用,添加失败";
            return Result.addFail(message);
        }
        //无重复，可以直接添加
        deviceService.add(bean);
        return Result.addSuccess(bean);

    }
    @SysLog("删除设备")
    @DeleteMapping("/devices/{id}")
    @RequiresPermissions("info:device")
    public String delete(@PathVariable("id") Long id) throws Exception {
        deviceService.delete(id);
        return null;
    }
    @SysLog("修改设备")
    @PutMapping("/devices")
    @RequiresPermissions("info:device")
    public Object update(@RequestBody Device bean) throws Exception {

        int existCode=deviceService.isExist(bean);
        if (existCode==1){
            String message ="控制系统编号已经被使用,更新失败";
            return Result.addFail(message);
        }
        if (existCode==2){
            String message ="北斗卡号已经被使用,更新失败";
            return Result.addFail(message);
        }
        //无重复，可以直接添加
        deviceService.update(bean);
        return Result.addSuccess(bean);
    }

}
